package com.yunfei.blog_mybatis.web.controller;

import com.yunfei.blog_mybatis.web.dao.ZhangyunfeiBlogMapper;
import com.yunfei.blog_mybatis.web.dataobject.ZhangyunfeiBlog;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: yunfei
 * @Date: 16:09
 */
@RestController
public class BlogController {
   @Autowired  ZhangyunfeiBlogMapper zhangyunfeiBlogMapper;
    @RequestMapping(path = "/insert")
    //对数据增加操作
    public String insert(@RequestParam("article_id") Long article_id, @RequestParam("author") String author,
                         ZhangyunfeiBlog zhangyunfeiBlog) {
        if(zhangyunfeiBlogMapper.selectByPrimaryKey(article_id,author).getArticleId()==null){
            zhangyunfeiBlogMapper.insert(zhangyunfeiBlog);
            return "success";
        }else{
            return "都有这个文章id了，你还想插入啥";
        }
    }
    @RequestMapping(path="/delete")
    public String delete(@RequestParam("article_id") Long article_id  ,@RequestParam("author") String author){

        if(zhangyunfeiBlogMapper.selectByPrimaryKey(article_id,author).getArticleId()!=null){
            zhangyunfeiBlogMapper.deleteByPrimaryKey(article_id, author);
            return "删除成功";
        }else{
            return "这个id都没有,你想删个啥";
        }
    }
    @RequestMapping(path="/query")
    public String query(@RequestParam("article_id") Long article_id,@RequestParam("author") String author){
        ZhangyunfeiBlog blog = zhangyunfeiBlogMapper.selectByPrimaryKey(article_id, author);
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(blog.getArticleId()+blog.getAuthor()+
                blog.getGmtCreated()+blog.getTitle()+blog.getWordCount()+
                blog.getViewNum()+blog.getCollectionNum()+blog.getLikeNum()+blog.getText());
        return stringBuilder.toString();
    }
    @RequestMapping(path = "/querymyblog")
    public String queryMyblog(@Param("author") String author){
                List<ZhangyunfeiBlog> blogList=zhangyunfeiBlogMapper.selectMyBlog(author);
                StringBuilder stringBuilder=new StringBuilder();
        for ( ZhangyunfeiBlog item: blogList
             ) {
            stringBuilder.append(item.getArticleId()+item.getAuthor()+item.getTitle()+
                    item.getGmtCreated()+item.getWordCount()+item.getLikeNum()+item.getCollectionNum()
            +item.getViewNum()+item.getText());
        }
        return stringBuilder.toString();
    }
    @RequestMapping(path = "/querybypage")
    public List queryByPage(){
        return zhangyunfeiBlogMapper.selectbypage();

    }

    @RequestMapping(path = "/update")
    public String update(@RequestParam("article_id") Long article_id, ZhangyunfeiBlog zhangyunfeiBlog) {
        zhangyunfeiBlogMapper.updateByPrimaryKey(zhangyunfeiBlog);
        return null;

    }


}
